package com.servlet;

import com.dao.StudentDao;

import com.vo.StudentVo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.lang.String;

public class UpdateStudentServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public UpdateStudentServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String studentNum = request.getParameter("userId");
        StudentDao sd = new StudentDao();

        StudentVo sv = sd.selectAll(studentNum);
        request.setAttribute("sv", sv);
        request.getRequestDispatcher("stu/update.jsp").forward(request, response);

    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        StudentVo sv = new StudentVo();
        //String stuId = request.getParameter("id");
        String stuNo = request.getParameter("id");

        String stuName = request.getParameter("studentName");
        String sex = request.getParameter("sex");
        String aClass = request.getParameter("class");
        String password = request.getParameter("password");
        String idcard = request.getParameter("idcard");

        sv.setStudentNum(stuNo);
        sv.setStudentName(stuName);
        sv.setSex(sex);
        sv.setStudentClass(aClass);
        sv.setPassword(password);
        sv.setIdCard(idcard);

        StudentDao sd = new StudentDao();
        sd.update(sv);
        ArrayList<StudentVo> list = sd.selectStudent();
        request.setAttribute("list", list);
        request.getRequestDispatcher("stu/studentList.jsp").forward(request, response);

    }


    public void init() throws ServletException {
        // Put your code here
    }

}
